package com.yeswater.multithread.chapter7;

/**
 * author : yeswater
 * date : 2023/11/5
 * <p>
 * 多级关联
 */

public class Run_7_2_2 {
    public static void main(String[] args) throws InterruptedException {
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup group = new ThreadGroup(mainGroup, "A"); // parent ThreadGroup
        Thread thread = new Thread(group, () -> {
            try {
                System.out.println("runMethod!");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.setName("Z");
        thread.start();
        ThreadGroup[] listGroup = new ThreadGroup[Thread.currentThread().getThreadGroup().activeCount()];
        Thread.currentThread().getThreadGroup().enumerate(listGroup);
        System.out.println("main线程中子线程组数:" + listGroup.length);
        System.out.println(listGroup[0].getName());
        Thread[] listThread = new Thread[listGroup[0].activeCount()];
        listGroup[0].enumerate(listThread);
        System.out.println(listThread[0].getName());
    }
}
